/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.ban.news.sender.test;

import cn.ac.iie.di.ban.pload.PLoader;
import cn.ac.iie.di.ban.pload.Status;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.HashMap;
import java.util.Random;

/**
 *
 * @author zwlin
 */
public class test {

    static String test1 = "{\"msg_type\":\"1\",\"total_pushed_num\":\"1\",\"record_list\":[{\"DOMAIN\":\"www.test.com\",\n"
            + "\"CHANNEL\":\"1\",\n"
            + "\"ORIGINURL\":\"adbija\",\n"
            + "\"AUTHOR\":\"adbija\",\n"
            + "\"TITLE\":\"adbija\",\n"
            + "\"TEXT\":\"adbija\",\n"
            + "\"PUBLISHTIME\":\"20180901160500\",\n"
            + "\"PHOTOURLS\":[\"adbija001\",\"adbija002\",\"adbija003\"],\n"
            + "\"VIDEOURLS\":[\"adbija021==\",\"adbija022\",\"adbija023\"],\n"
            + "\"COLUMN\":[\"adbija031\",\"adbija032\",\"adbija033\"],\n"
            + "\"SPECIALNAME\":\"\",\n"
            + "\"ISDEL\":\"1\",\n"
            + "\"DELTIME\":\"20180901160600\",\n"
            + "\"PVNUM\":\"132\",\n"
            + "\"UVNUM\":165,\n"
            + "\"PRAISEDNUM\":303\n"
            + "\n"
            + "}]}";
    static String test2 = "{\"msg_type\":\"2\",\"total_pushed_num\":\"1\",\"record_list\":[{\"DOMAIN\":\"www.test.com\",\n"
            + "\"ORIGINURL\":\"likhsuvrvi857bh4lamkjrtv\",\n"
            + "\"POSTCHANNEL\":\"1\",\n"
            + "\"POSTID\":\"sadoadbijaowrgh==\",\n"
            + "\"FPOSTID\":\"sadoadbijaowrgh=\",\n"
            + "\"POSTTIME\":\"20180901160400\",\n"
            + "\"POSTCONT\":\"adbijaowrgavw684sbe614sae31bsserbtsber1e6esb61eb6beybese4==\",\n"
            + "\"USERID\":\"\",\n"
            + "\"POSTIP\":\"192.168.11.95\",\n"
            + "\"POSTSTATUS\":\"0\",\n"
            + "\"POSTDELTIME\":\"20180901160600\"\n"
            + "\n"
            + "}]}";
    static String test3 = "{\"msg_type\":\"3\",\"total_pushed_num\":\"1\",\"record_list\":[{\n"
            + "\"DOMAIN\":\"www.test.com\",\n"
            + "\"CHANNEL\":\"0\",\n"
            + "\"ORIGINURL\":\"likhsuvrvi857bh4lamkjrtv=\",\n"
            + "\"PVNUM\":1024,\n"
            + "\"UVNUM\":2038}\n"
            + "]\n"
            + "}";
    static String test4 = "{\"msg_type\":\"4\",\"total_pushed_num\":\"1\",\"record_list\":[{\"DOMAIN\":\"www.test.com\",\"POSTCHANNEL\":\"1\",\"POSTID\":\"a3wf09jfsg25==\",\"LIKENUM\":5}]}";

    static String test_user = "{\n"
            + "	\"msg_type\": \"5\",\n"
            + "	\"total_pushed_num\": \"1\",\n"
            + "	\"record_list\": [{\n"
            + "		\"DOMAIN\": \"www.test.com\",\n"
            + "		\"USERID\": \"bidkspovusnteg5==\",\n"
            + "		\"USERNAME\": \"bidkspovusnteg5\",\n"
            + "		\"REGISTERTIME\": \"19990101165210\",\n"
            + "		\"HEADURL\": \"bidkspovusnteg5=\",\n"
            + "		\"EMAIL\": \"helloa@alibpaba.com\",\n"
            + "		\"USERPHONE\": \"8618610110100\",\n"
            + "		\"USERQQ\": \"1232165452\",\n"
            + "		\"USERSTATE\": \"2\",\n"
            + "		\"PROCESSINGTIME\": \"20010110121510\"\n"
            + "}"
            + "]\n"
            + "}";
    static private String pre = "{\n"
            + "	\"msg_type\": \"5\",\n"
            + "	\"total_pushed_num\": \"1\",\n"
            + "	\"record_list\": [{\n"
            + "		\"DOMAIN\": \"www.test.com\",\n"
            + "		\"USERID\": \"bidkspovusnteg5==\",\n"
            + "		\"USERNAME\": \"bidkspovusnteg5\",\n"
            + "		\"REGISTERTIME\": \"19990101165210\",\n"
            + "		\"HEADURL\": \"bidkspovusnteg5=\",\n"
            + "		\"EMAIL\": \"helloa@alibpaba.com\",\n"
            + "		\"USERPHONE\": \"8618610110100\",\n"
            + "		\"USERQQ\": \"1232165452\",\n"
            + "		\"USERSTATE\": \"2\",\n"
            + "		\"PROCESSINGTIME\": \"20010110121510\"\n"
            + "}";
    static private String mid = ",{"
            + "		\"DOMAIN\": \"www.test.com\",\n"
            + "		\"USERID\": \"bidkspovusnteg5==\",\n"
            + "		\"USERNAME\": \"bidkspovusnteg5\",\n"
            + "		\"REGISTERTIME\": \"19990101165210\",\n"
            + "		\"HEADURL\": \"bidkspovusnteg5=\",\n"
            + "		\"EMAIL\": \"helloa@alibaba.com\",\n"
            + "		\"USERPHONE\": \"8618610110100\",\n"
            + "		\"USERQQ\": \"1232165452\",\n"
            + "		\"USERSTATE\": \"2\",\n"
            + "		\"PROCESSINGTIME\": \"20010110121510\"\n"
            + "	}";
    static private String suffix = "]\n"
            + "}";

    public static void main(String[] args) throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException, Exception {
        //整个过程是线程安全的，可在多个线程同时使用。
        //获取发送工具。参数是企业唯一key
        PLoader sender = PLoader.getInstance("5700e4ab800b28a99d3d976eaffe9469");
        try {
            //发送。参数是json格式的数据内容。
//			sender.send("data_json 0");
//			sender.send("data_json 1");
            //发送的结果
            Status status = null;
            Random rand = new Random();
            int a = 0;
            HashMap<Integer, String> midMap = new HashMap<>();
            for (int ttmp = 0; ttmp < 10000; ++ttmp) {
                for (int i = 0; i < 10000; ++i) {

                    Thread.sleep(1000 + rand.nextInt(1000));
                    String json = pre;
                    int midLen = 230 + rand.nextInt(70);
                    json += getMidString(midMap, midLen);
//                    for (int j = 0; j < rand.nextInt(70); ++j) {
//                        json += mid;

//                    }
                    json += suffix;
                    status = sender.send(test1);
                    status = sender.send(test2);
                    status = sender.send(test3);
                    status = sender.send(test4);
                    status = sender.send(test_user);

                    //发送成功
                    if (status.success) {
                        if (true || ++a > 100) {
                            System.out.println(new Date().toString() + "\t" + a + "\tsuccessful");
                            a = 0;
                        }
                    } else {
                        //错误说明
                        System.out.println(status.reasonPhrase);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            //处理可能的报错。
        }
        //发送结束后，结束sender。
        //注意：相同的企业唯一key对应的发送工具是同一个实例，一旦close，需要重新start才能使用。
        sender.close();
        //可以不调用sender.close，在程序结束时调用closeAll，自动关闭所有发送工具。
        //PLoader.closeAll();

    }

    private static String getMidString(HashMap<Integer, String> map, int len) {
        if (map.containsKey(len)) {
            return map.get(len);
        } else if (len == 1) {
            map.put(1, mid);
            return mid;
        } else {
            String tmp = getMidString(map, len / 2);
            tmp += tmp;
            if (len % 2 == 0) {
            } else {
                tmp += mid;
            }
            map.put(len, tmp);
            return tmp;
        }
    }
}
